/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.bo.servlet;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import unilearn.bo.AccessValidator;
import unilearn.vo.Person;
import unilearn.vo.Role;

/**
 *
 * @author Yat
 */
public class LoginServlet extends HttpServlet {

    private String staffPage;
    private String studentPage;
    private String failurePage;
    private AccessValidator validator;

    public void init() throws ServletException {
        ServletConfig sc = this.getServletConfig();
        staffPage = sc.getInitParameter("staffPage");
        studentPage = sc.getInitParameter("studentPage");
        failurePage = sc.getInitParameter("failurePage");
        WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
        validator = (AccessValidator) context.getBean("validator");
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null;
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
        try {
            String uName = request.getParameter("userName");
            String pw = request.getParameter("uPassword");

            if (((uName == null) || uName.trim().equals("")) || ((pw == null) || (pw.trim().equals("")))) {
                throw new Exception("Please enter your user name or password.");
            }

            Person person = validator.validate(uName, pw);
            Role role = validator.getRole(person);

            request.getSession().setAttribute("person", person);
            request.getSession().setAttribute("role", role);

            if (role.getName().equals("student")) {
                System.out.println(" ========================= [ Student ] ========================= ");
                page = studentPage;
            } else if (role.getName().equals("staff")) {
                System.out.println(" ========================= [ Staff ] ========================= ");
                page = staffPage;
            } else if (role.getName().equals("administrator")) {
                System.out.println(" ========================= [ Admin ] ========================= ");
                page = staffPage;
            } else if (role.getName().equals("lecturer")) {
                System.out.println(" ========================= [ Lecturer ] ========================= ");
                page = staffPage;
            }

            System.out.println(" ========================= [ Login Successful. ] ========================= ");
        } catch (Exception e) {
            System.out.println(" ========================= [ Login Failure. ] ========================= ");
            page = failurePage;
            System.out.println(getClass().getName() + "[processRequest]\n" + e.toString());
        } finally {
            if (page == null) {
                page = failurePage;
            }
            System.out.println(" ========================= [ Redirect(" + page + ") ] ========================= ");
            response.sendRedirect(page);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
